﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JeanFish.Blog;
using JeanFish.Blog.Storage;
using Business=JeanFish.Blog.Business;
using Storage = JeanFish.Blog.Storage;
using JeanFish.Blog.Storage.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Configuration;

namespace JeanFish.UnitTest.Blog
{
    [TestClass]
    public class BlogTest
    {
        [TestInitialize]
        public void Initialize()
        {
            StubIBlogMediaProvider mediaProvider = new StubIBlogMediaProvider();
            mediaProvider.SaveMediaBlogStringStream = (blog, mediaName, stream) => {
                return new Uri(string.Format("http://test.com/{0}/{1}{2}", blog.ID, mediaName,Guid.NewGuid().ToString("N")));
            };
            BlogService.SetBlogProvider(BlogProviderHelper.GetDefaultDataProvider("jeanFish.blog/data")
            ,mediaProvider);
            BlogService.Current.LoadBlogAsync();
        }
        [TestMethod]
        public void BlogSTest()
        {
            var blog = BlogService.Current.Blog;

            Business.Post newPost = new Business.Post(blog);
            Assert.IsTrue(newPost.IsNew);
            Assert.IsTrue(newPost.IsChanged);
            newPost.AcceptChanges();
            Assert.IsFalse(newPost.IsNew);
            Assert.IsFalse(newPost.IsChanged);
            var updated = newPost.ModifiedTime;
            newPost.Text = string.Format("Now is {0}.", DateTime.Now.ToString());
            Assert.IsTrue(newPost.IsChanged);
            newPost.AcceptChanges();
            Assert.IsTrue(newPost.ModifiedTime > updated);
            Assert.IsFalse(newPost.IsChanged);
            newPost.Delete();
            Assert.IsTrue(newPost.IsChanged);
            int pre = blog.ListPosts(new Paging(0,int.MaxValue)).Count;
            newPost.AcceptChanges();
            Assert.AreEqual(pre - 1, blog.ListPosts(new Paging(0, int.MaxValue)).Count);
            BlogService.Current.LoadBlogAsync();
            blog = BlogService.Current.Blog;
            Assert.AreEqual(pre - 1, blog.ListPosts(new Paging(0, int.MaxValue)).Count);
        }
        [TestMethod]
        public void BlogConcurrencyTest()
        {
            var blog1 = BlogService.Current.Blog;
            int pc1=blog1.ListPosts(new Paging(0,int.MaxValue)).Count;
            BlogService.Current.LoadBlogAsync();
            var blog2 = BlogService.Current.Blog;
            int pc2 = blog2.ListPosts(new Paging(0, int.MaxValue)).Count;
            Business.Post p1 = new Business.Post(blog1)
            {
                Title = "Test1"
            };
            p1.AcceptChanges();
            Assert.AreEqual(pc2+1, blog2.ListPosts(new Paging(0, int.MaxValue)).Count);
            var p1inb2 = blog2.GetPostAsync(p1.ID);
            Assert.IsNotNull(p1inb2);
            Assert.AreEqual(blog1.ListPosts(new Paging(0, int.MaxValue)).Count, blog2.ListPosts(new Paging(0, int.MaxValue)).Count);
            p1inb2.Delete();
            p1inb2.AcceptChanges();
            Assert.AreEqual(pc2, blog1.ListPosts(new Paging(0, int.MaxValue)).Count);
        }
    }
}
